package com.learn.rpc.version.version2;

import com.learn.rpc.common.AdminUser;
import com.learn.rpc.common.FinalParam;
import com.learn.rpc.common.IAdminUserService;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * 将socket调用进行简单封装后再进行直接远程RPC调用,通过字符串和基础类型数据进行二次拼接成对象
 * @author wangxing
 * @version 2020/8/15 7:03 Administrator
 */
public class RpcClient {
    public static void main(String[] args) throws IOException {
        AdminUserServiceImplSocket serviceImplSocket = new AdminUserServiceImplSocket();
        System.out.println(serviceImplSocket.findAdminUserById(12345));
    }

}
class AdminUserServiceImplSocket implements IAdminUserService{

    @Override
    public AdminUser findAdminUserById(Integer id) {
        try {
            Socket socket = new Socket(FinalParam.HOST,FinalParam.PORT);
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataOutputStream.writeInt(id);
            dataOutputStream.flush();

            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            Integer resultId = dataInputStream.readInt();
            String name = dataInputStream.readUTF();
            AdminUser adminUser = new AdminUser(resultId,name);
            dataInputStream.close();
            dataOutputStream.close();
            socket.close();
            return adminUser;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public AdminUser findAdminUserByName(String name) {
        return null;
    }

    @Override
    public String getNameAndId(Integer id, String name) {
        return null;
    }
}
